package com.zang.blogz.strategy.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONParser;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zang.blogz.config.QQConfigProperties;
import com.zang.blogz.config.satoken.GiteeConfigProperties;
import com.zang.blogz.constant.SocialLoginConstant;
import com.zang.blogz.dto.GiteeUserInfoDTO;
import com.zang.blogz.dto.SocialTokenDTO;
import com.zang.blogz.dto.SocialUserInfoDTO;
import com.zang.blogz.enmus.LoginTypeEnum;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;

/**
 * @Author: ZVerify
 * @Description: Gitee第三方登录
 * @DateTime: 2022/9/18 20:28
 **/
@Service("giteeLoginStrategyImpl")
public class GiteeLoginStrategyImpl extends AbstractSocialLoginStrategyImpl{

    @Autowired
    private GiteeConfigProperties giteeConfigProperties;

    @Override
    public SocialTokenDTO getSocialToken(String data) throws IOException {

        String url = String.format(SocialLoginConstant.OAUTH2_GITEE_CODE, giteeConfigProperties.getClientId(), giteeConfigProperties.getRedirectUri(), giteeConfigProperties.getClientSecret(), data);

        CloseableHttpClient build = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse response = build.execute(httpPost);

        String json = EntityUtils.toString(response.getEntity());

        JSONObject jsonObject = JSON.parseObject(json);
        String access_token = jsonObject.getString(SocialLoginConstant.ACCESS_TOKEN);
        return SocialTokenDTO.builder().accessToken(access_token).openId(data).loginType(LoginTypeEnum.GITEE.getType()).build();
    }

    @Override
    public SocialUserInfoDTO getSocialUserInfo(SocialTokenDTO socialTokenDTO) throws IOException {

        String urlUser = String.format(SocialLoginConstant.OAUTH2_GITEE_USERINFO, socialTokenDTO.getAccessToken());

        HttpClient httpClientUser = HttpClientBuilder.create().build();
        HttpGet httpPostUser = new HttpGet(urlUser);

        HttpResponse responseUser = httpClientUser.execute(httpPostUser);

        String user = EntityUtils.toString(responseUser.getEntity());

        GiteeUserInfoDTO giteeUserInfoDTO = JSONUtil.toBean(user, GiteeUserInfoDTO.class);

        return SocialUserInfoDTO.builder()
                .nickname(giteeUserInfoDTO.getName())
                .avatar(giteeUserInfoDTO.getAvatarUrl())
                .build();
    }
}
